package org.biubiu0002.baiblog.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.biubiu0002.baiblog.common.utils.PageUtils;
import org.biubiu0002.baiblog.common.utils.R;
import org.biubiu0002.baiblog.core.annotations.PermissionIgnore;
import org.biubiu0002.baiblog.core.model.entity.SysPermissionEntity;
import org.biubiu0002.baiblog.core.model.vo.PermissionVo;
import org.biubiu0002.baiblog.core.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 权限表
 *
 * @author biubiu0002
 * @email 1007918011@qq.com
 * @date 2022-05-06 00:50:38
 */
@RestController
@RequestMapping("/syspermission")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

    @GetMapping("/listAll")
    @PermissionIgnore
    public R listAll(){
        List<SysPermissionEntity> list = sysPermissionService.list(new QueryWrapper<SysPermissionEntity>().eq("is_deleted", 0));
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysPermissionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		SysPermissionEntity sysPermission = sysPermissionService.getById(id);
        return R.ok().put("sysPermission", sysPermission);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody PermissionVo permissionSaveReqVo){
        Preconditions.checkArgument(StringUtils.isNotBlank(permissionSaveReqVo.getMethod()),"方法全类名不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(permissionSaveReqVo.getDescription()),"资源描述不能为空");
        Preconditions.checkArgument(permissionSaveReqVo.getDescription().length()<=50,"资源描述长度不能超过50");
		sysPermissionService.save(permissionSaveReqVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/{permissionId}")
    public R update(@PathVariable("permissionId") String permissionId,
            @RequestBody PermissionVo permissionVo){
        Preconditions.checkArgument(StringUtils.isNotBlank(permissionVo.getMethod()),"方法全类名不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(permissionVo.getDescription()),"资源描述不能为空");
        Preconditions.checkArgument(permissionVo.getDescription().length()<=50,"资源描述长度不能超过50");
        permissionVo.setPermissionId(permissionId);
        sysPermissionService.update(permissionVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("core:syspermission:delete")
    public R delete(@RequestBody Integer[] ids){
		sysPermissionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
